import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/components/Login.vue'
import Home from '@/components/Home.vue'
import Welcome from '@/view/Welcome.vue'
import User from '@/view/user/User.vue'
import Rights from '@/view/power/Rights.vue'
import Roles from '@/view/power/Roles.vue'
import Cate from '@/view/goods/Cate.vue'
import Params from '@/view/goods/Params.vue'
import List from '@/view/goods/List.vue'
import Add from '@/view/goods/Add.vue'
import Edit from '@/view/goods/Edit.vue'
import Order from '@/view/order/Order.vue'
import Report from '@/view/report/Report.vue'
Vue.use(VueRouter)

const routes = [
  { path: '', component: Login },
  { path: '/login', component: Login },
  {
    path: '/home', component: Home,
    children: [
      { path: '', component: Welcome },
      { path: '/users', component: User },
      { path: '/rights', component: Rights },
      { path: '/goods', component: List },
      { path: '/goods/add', component: Add },
      { path: '/goods/edit', component: Edit },
      { path: '/params', component: Params },
      { path: '/categories', component: Cate },
      { path: '/roles', component: Roles },
      { path: '/orders', component: Order },
      { path: '/reports', component: Report },
    ]
  },
]

const router = new VueRouter({
  routes
})

// 全局导航守卫
router.beforeEach(function (to, from, next) {
  // 如果是访问登录页面，直接放行
  if (to.path === '/login') return next()
  // 否则 判断是否有token
  const token = sessionStorage.getItem('token')
  if (!token) return next('/login')
  // 有token
  next()
})

export default router
